//
//  CZComposeTextView.swift
//  weiBo
//
//  Created by apple on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZComposeTextView: UITextView {

    //占位文字
    private lazy var placeHolderLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.text = "哈哈哈哈哈哈哈哈哈哈"
        label.sizeToFit()
        label.numberOfLines = 0
        label.textColor = UIColor.lightGrayColor()
        
        
        return label
    }()
    
    var placeHolder: String? {
        didSet {
            placeHolderLabel.text = placeHolder
        }
    }
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setUpUI()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChage", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    @objc private func textChage() {
        placeHolderLabel.hidden = hasText()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   
    
    private func setUpUI(){
        addSubview(placeHolderLabel)
        placeHolderLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeHolderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        placeHolderLabel.frame.origin.x = 5
        placeHolderLabel.frame.origin.y = 8
        
        
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}
